﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;
using CustomUIControls;


namespace SalesVMT
{
    public partial class frmStoreStatusList : DockContent
    {
        int pageSize = UIParamater.PageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int _Item_ID;
        

        public int Item_ID
        {
            get { return _Item_ID; }
            set { _Item_ID = value; }
        }
        string _Item_Code;

        public string Item_Code
        {
            get { return _Item_Code; }
            set { _Item_Code = value; }
        }
        string _Item_Name;

        public string Item_Name
        {
            get { return _Item_Name; }
            set { _Item_Name = value; }
        }

        string _Unit_Name;

        public string Unit_Name
        {
            get { return _Unit_Name; }
            set { _Unit_Name = value; }
        }
        decimal _Item_PurchasePrice;

        public decimal Item_PurchasePrice
        {
            get { return _Item_PurchasePrice; }
            set { _Item_PurchasePrice = value; }
        }
        decimal _Item_WholesalePrice;

        public decimal Item_WholesalePrice
        {
            get { return _Item_WholesalePrice; }
            set { _Item_WholesalePrice = value; }
        }
        decimal _Item_RetailPrice;

        public decimal Item_RetailPrice
        {
            get { return _Item_RetailPrice; }
            set { _Item_RetailPrice = value; }
        }
        private string _KeyWord;

        public string KeyWord
        {
            get { return _KeyWord; }
            set { _KeyWord = value; }
        }
        bool selected;
        public frmStoreStatusList(bool _selected, string keyword)
        {
            InitializeComponent();
            selected = _selected;
            txtKeyWord.Text = keyword;
            DisplayFrame();
            if (_selected)
                cboStatus.Enabled = false;
            cboStatus.SelectedIndex = 0;
            LoadData();
        }


        private void DisplayFrame()
        {
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", 50, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "Item_Code", "Mã số MH", "", ColumnWidth.Code2, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_Name", "Tên MH", "", ColumnWidth.ItemName, true, true);
            UISetting.AddColumn(ref dgvList1, "Quantity", "SL", "R", ColumnWidth.Quantity, true, true);
            UISetting.AddColumn(ref dgvList1, "CalculationUnit_Name", "ĐVT", "C", ColumnWidth.Unit, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_PurchasePrice", "Giá mua", "R", ColumnWidth.Price, false, true);
            UISetting.AddColumn(ref dgvList1, "Item_WholesalePrice", "Giá BB", "R", ColumnWidth.Price, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_RetailPrice", "Giá BL", "R", ColumnWidth.Price, true, true);
            UISetting.AddColumn(ref dgvList1, "Origin_Name", "Xuất xứ", "C", ColumnWidth.OriginName, true, true);
            UISetting.AddColumn(ref dgvList1, "Total", "Tổng trị giá", "R", ColumnWidth.TotalPrice, true, true);
            
        }
        private void LoadData()
        {
            DataTable dt = new DataTable();
            if (cboStatus.SelectedIndex == 0)//Đang tồn
                dt = InventoryDB.GetList(out itemCount, UISetting.GetKeyWord(txtKeyWord.Text), currPage, pageSize).Tables[0];
            if(cboStatus.SelectedIndex ==1)//Đã nhập
                dt = ReceiptDetailsDB.GetList(out itemCount, UISetting.GetKeyWord(txtKeyWord.Text), currPage, pageSize).Tables[0];
            if (cboStatus.SelectedIndex == 2)//Đã Xuất
                dt = ShipmentDetailsDB.GetList(out itemCount, UISetting.GetKeyWord(txtKeyWord.Text), currPage, pageSize).Tables[0];
            
            PageIndex();
            dgvList1.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvList1.Rows.Insert(i, 1);

                dgvList1.Rows[i].Cells["RowNumber"].Value = dt.Rows[i]["RowNumber"].ToString();
                dgvList1.Rows[i].Cells["Item_ID"].Value = dt.Rows[i]["Item_ID"].ToString();
                dgvList1.Rows[i].Cells["Item_Code"].Value = dt.Rows[i]["Item_Code"].ToString();
                dgvList1.Rows[i].Cells["Item_Name"].Value = dt.Rows[i]["Item_Name"].ToString();
                dgvList1.Rows[i].Cells["CalculationUnit_Name"].Value = dt.Rows[i]["CalculationUnit_Name"].ToString();
                dgvList1.Rows[i].Cells["Item_PurchasePrice"].Value = Convert.ToDecimal(dt.Rows[i]["Item_PurchasePrice"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Item_WholesalePrice"].Value = Convert.ToDecimal(dt.Rows[i]["Item_WholesalePrice"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Item_RetailPrice"].Value = Convert.ToDecimal(dt.Rows[i]["Item_RetailPrice"]).ToString("#,##0");
                dgvList1.Rows[i].Cells["Origin_Name"].Value = dt.Rows[i]["Origin_Name"].ToString();


                if (cboStatus.SelectedIndex == 0&&dt.Rows[i]["Quantity"] == DBNull.Value)
                {
                    dgvList1.Rows[i].Cells["Quantity"].Value = dt.Rows[i]["ReceiptQuantity"].ToString();
                }
                else
                    dgvList1.Rows[i].Cells["Quantity"].Value = dt.Rows[i]["Quantity"].ToString();

                if (cboStatus.SelectedIndex == 0 && dt.Rows[i]["Total"] == DBNull.Value)
                {
                    dgvList1.Rows[i].Cells["Total"].Value = Convert.ToDecimal(dt.Rows[i]["ReceiptTotal"]).ToString("#,##0");
                }
                else
                    dgvList1.Rows[i].Cells["Total"].Value = Convert.ToDecimal(dt.Rows[i]["Total"]).ToString("#,##0");
                
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void dgvList1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (selected)
                {
                    _Item_ID = Convert.ToInt32(dgvList1["Item_ID", dgvList1.SelectedCells[0].RowIndex].Value);
                    _Item_Code = Convert.ToString(dgvList1["Item_Code", dgvList1.SelectedCells[0].RowIndex].Value);
                    _Item_Name = Convert.ToString(dgvList1["Item_Name", dgvList1.SelectedCells[0].RowIndex].Value);
                    _Unit_Name = Convert.ToString(dgvList1["CalculationUnit_Name", dgvList1.SelectedCells[0].RowIndex].Value);
                    _Item_PurchasePrice = Convert.ToDecimal(dgvList1["Item_PurchasePrice", dgvList1.SelectedCells[0].RowIndex].Value);
                    _Item_WholesalePrice = Convert.ToDecimal(dgvList1["Item_WholesalePrice", dgvList1.SelectedCells[0].RowIndex].Value);
                    _Item_RetailPrice = Convert.ToDecimal(dgvList1["Item_RetailPrice", dgvList1.SelectedCells[0].RowIndex].Value);
                    _KeyWord = txtKeyWord.Text;
                    this.DialogResult = DialogResult.OK;
                }
            }
        }




    }
}